#include "3rdhelper/qtsingleapplication/qtsingleapplication.h"
#include <QCommandLineParser>
#include "rfbserver.h"
#include "xdp/waylandintegration.h"
#include "debug.h"

int main(int argc, char *argv[])
{
    LogUtil::registerLogger(NULL);

    QtSingleApplication a(argc, argv);
    if (a.isRunning()) {
        qCCritical(KRFB) << "the program is running";
        return 1;
    }

    QCommandLineParser parser;
    parser.setApplicationDescription("udcp-krfb");
    parser.addVersionOption();
    const QCommandLineOption portOption(QStringList() << "p"
                                                      << "port",
                                        "The port we will be listening to", "NUM", "5900");
    parser.addOption(portOption);
    parser.process(a);

    bool ok;
    int port = parser.value(portOption).toInt(&ok);
    if (ok == false) {
        qCCritical(KRFB) << "port is not right!";
        return 1;
    }

    if (!WaylandIntegration::init()) {
        qCCritical(KRFB) << "wayland integration false";
        return 1;
    }

    RfbServer s;
    s.setListeningPort(port);
    s.start();

    return a.exec();
}
